package com.zrrd.jt_admin.order.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zrrd.jt_admin.common.entity.Order;
import com.zrrd.jt_admin.common.entity.User;
import com.zrrd.jt_admin.common.utils.DateFormatUtil;
import com.zrrd.jt_admin.common.vo.OrderItemVo;
import com.zrrd.jt_admin.common.page.OrderPageVo;
import com.zrrd.jt_admin.common.vo.OrderVo;
import com.zrrd.jt_admin.order.mapper.OrderMapper;
import com.zrrd.jt_admin.order.service.OrderItemService;
import com.zrrd.jt_admin.order.service.OrderService;
import com.zrrd.jt_admin.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanjl
 * @since 2024-07-11
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private OrderItemService orderItemService;

    @Resource
    private UserService userService;


    /**
     * 分页查询订单信息
     *
     * @param page
     * @param limit
     * @param tab
     * @param no
     * @param startTime
     * @param endTime
     * @param name
     * @param phone
     * @return
     */
    @Override
    public OrderPageVo findByPage(Integer page, Integer limit, String tab, String no, String startTime, String endTime, String name, String phone) {
        PageHelper.startPage(page, limit);

        System.out.println(no);
        System.out.println(startTime);
        System.out.println(endTime);
        System.out.println(name);
        System.out.println(phone);
        LambdaQueryChainWrapper<Order> lq = lambdaQuery();

        if (!"".equals(no)) {
            lq.eq(Order::getNo, no);
        }
        if (!"".equals(startTime)&&!"".equals(endTime)) {
            int sTime = Integer.parseInt(DateFormatUtil.date2TimeStamp(startTime, "yyyy-MM-dd"));
            //System.out.println(sTime);
            int eTime = Integer.parseInt(DateFormatUtil.date2TimeStamp(endTime, "yyyy-MM-dd"));
            System.out.println(sTime);
            System.out.println(eTime);
            lq.between(Order::getCreateTime,sTime,eTime);
        }
        switch (tab){
            case "all"://全部
                break;
            case "nopay"://待付款
                lq.isNull(Order::getPaymentMethod);
                lq.or().eq(Order::getPaymentMethod,"");
                break;
            case "noship"://待发货
//                lq.eq(Order::getShipStatus,"pending");
//                lq.isNotNull(Order::getPaymentNo).isNotNull(Order::getPaymentMethod);
//                lq.or().ne(Order::getPaymentNo,"").ne(Order::getPaymentMethod,"");
                lq.isNull(Order::getShipData).or().eq(Order::getShipData,"");
                break;
            case "shiped"://已发货
                lq.isNotNull(Order::getShipData).list();
                break;
            case "received"://已收货
                lq.eq(Order::getShipStatus,"received");
                break;
            case "finish"://已完成
                break;
            case "closed"://已关闭
                lq.eq(Order::getClosed,1);
                break;
            case "refunding"://退款中
                lq.ne(Order::getRefundStatus,"pending");
                break;
        }


        List<Order> list = lq.list();
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        List<OrderVo> orderVoList =pageInfo.getList().stream().map(order -> {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(order, orderVo);
            Integer orderId = order.getId();
            List<OrderItemVo> itemVos = orderItemService.findByOrderId(orderId);
            orderVo.setOrderItems(itemVos);
            Integer userId = order.getUserId();
            User user = userService.getById(userId);
            if (!"".equals(phone)) {
                user = userService.lambdaQuery().eq(User::getId,userId).eq(User::getPhone, phone).one();
            }
            orderVo.setUser(user);
            return orderVo;
        }).collect(Collectors.toList());
        OrderPageVo orderPageVo = new OrderPageVo();
        orderPageVo.setList(orderVoList);
        orderPageVo.setTotalCount(pageInfo.getTotal());

        return orderPageVo;
    }
}
